#include <cassert>
#include <typeinfo>
#include <algorithm>

class Holder
{
public:
   virtual ~Holder() {}
   
   virtual const std::type_info& GetType() = 0;

   virtual Holder *Clone() = 0;
};

template <class T>
class PlaceHolder : public Holder
{
public:
   PlaceHolder() {}

   PlaceHolder(const T& val)
      :_val(val)
   {}

   const std::type_info& GetType()
   {
      return typeid(_val);
   }

   Holder* Clone()
   {
      return new PlaceHolder<T>(_val);
   }

public:
   T _val;
};

class Any
{
public:
   Any()
      :_holder(nullptr)
   {}
   
   template<class T>
   Any(const T& val)
      :_holder(new PlaceHolder<T>(val))
   { }

   Any(const Any& other)
      :_holder(_holder = other._holder == nullptr ? nullptr : (other._holder)->Clone())
   {}

   Any& Swap(Any& other)
   {
      std::swap(other._holder, _holder);
      return *this;
   }

   template<class T>
   Any& operator=(const T& val)
   {
      Any(val).Swap(*this);
      return *this;
   }

   Any& operator=(const Any& other)
   {
      _holder = (other._holder)->Clone();
      return *this;
   }

   const std::type_info& GetType()
   {
      return _holder->GetType();
   }

   template<class T>
   T& GetValue()
   {
      assert(typeid(T) == typeid(((PlaceHolder<T>*)_holder)->_val));
      return (((PlaceHolder<T>*)_holder)->_val);
   }


private:
   Holder *_holder;
};
